﻿using System;
using System.Windows.Browser;

using SBPweb.RIA.Client.Contracts.Base;
using SBPweb.RIA.Client.Contracts.Models;

namespace SBPweb.RIA.Client.Controls.Models
{

    public class ScreenModel : DependencyObjectBase, IScreenModel
    {

        #region Fields

        private ScriptObject soScreen;

        #endregion

        #region Constructor logic

        public ScreenModel()
        {
            soScreen = HtmlPage.Window.GetProperty("screen") as ScriptObject;
            HtmlPage.Window.AttachEvent("onscroll", new EventHandler<HtmlEventArgs>(OnScroll));
            HtmlPage.Window.AttachEvent("onresize", new EventHandler<HtmlEventArgs>(OnResize));
        }

        #endregion

        #region Properties

        public double ClientWidth
        {
            get
            {
                return (double)HtmlPage.Document.Body.GetProperty("clientWidth");
            }
        }

        public double ClientHeight
        {
            get
            {
                return (double)HtmlPage.Document.Body.GetProperty("clientHeight");
            }
        }

        public double ScrollLeft
        {
            get
            {
                return (double)HtmlPage.Document.Body.GetProperty("scrollLeft");
            }
        }

        public double ScrollTop
        {
            get
            {
                return (double)HtmlPage.Document.Body.GetProperty("scrollTop");
            }
        }

        public double ScrollWidth
        {
            get
            {
                return (double)HtmlPage.Document.Body.GetProperty("scrollWidth");
            }
        }

        public double ScrollHeight
        {
            get
            {
                return (double)HtmlPage.Document.Body.GetProperty("scrollHeight");
            }
        }

        public double ScreenWidth
        {
            get
            {
                return (double)soScreen.GetProperty("width");
            }
        }

        public double ScreenHeight
        {
            get
            {
                return (double)soScreen.GetProperty("height");
            }
        }

        public double ScreenLeft
        {
            get
            {
                return (double)HtmlPage.Window.GetProperty("screenLeft");
            }
        }

        public double ScreenTop
        {
            get
            {
                return (double)HtmlPage.Window.GetProperty("screenTop");
            }
        }

        public double AvailableScreenWidth
        {
            get
            {
                return (double)soScreen.GetProperty("availWidth");
            }
        }

        public double AvailableScreenHeight
        {
            get
            {
                return (double)soScreen.GetProperty("availHeight");
            }
        }

        #endregion

        #region Events

        public event EventHandler<HtmlEventArgs> Resized;

        public event EventHandler<HtmlEventArgs> Scrolled;

        #endregion

        #region Event handlers
        
        protected void OnResize(object sender, HtmlEventArgs e)
        {
            if (Resized != null)
            {
                Resized(sender, e);
            }
        }

        protected void OnScroll(object sender, HtmlEventArgs e)
        {
            if (Scrolled != null)
            {
                Scrolled(sender, e);
            }
        }

        #endregion

    }

}
